package net.maku.module.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import net.maku.module.business.dao.DictionariesSubsetDao;
import net.maku.module.business.dao.jpa.DictionariesSubsetRepository;
import net.maku.module.business.entity.DictionariesSubsetEntity;
import net.maku.module.business.service.DictionariesSubsetService;
import net.maku.newbase.NewBaseServiceImpl;
import net.maku.newbase.annotation.redis.RedisRW;
import net.maku.newbase.util.redis.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 业务_字典子类
 *
 * @author
 */
@Service
@AllArgsConstructor
public class DictionariesSubsetServiceImpl extends NewBaseServiceImpl<DictionariesSubsetDao, DictionariesSubsetEntity> implements DictionariesSubsetService {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private DictionariesSubsetRepository dictionariesSubsetRepository;

    @Override
    @RedisRW(isList = true, tableField = "name", c = DictionariesSubsetEntity.class)
    public JSONArray findByDictId(Long dictId) {
        return JSONArray.parseArray(JSON.toJSONString(dictionariesSubsetRepository.findByDictIdOrderBySortAsc(dictId)));
    }

    @Override
    public Map<Long, String> dict(JSONArray jsonArray) {
        List<DictionariesSubsetEntity> list= jsonArray.toJavaList(DictionariesSubsetEntity.class);
        return list.stream().collect(Collectors.toMap(DictionariesSubsetEntity::getId,DictionariesSubsetEntity::getName));
    }
}